require "TimedActions/ISFitnessAction"
require "TimedActions/ISCraftAction"
require "TimedActions/ISPlowAction"
require "TimedActions/ISPlowAction"
require "TimedActions/ISApplyBandage"
require "Fishing/TimedActions/ISFishingAction"
require "Foraging/ISForageAction"
require "Traps/TimedActions/ISCheckTrapAction"

local ISFitnessAction_stop = ISFitnessAction.stop
local ISFitnessAction_perform = ISFitnessAction.perform

function ISFitnessAction:stop()
    ISFitnessAction_stop(self)
    local time = getGameTime():getCalender():getTimeInMillis() - self.startMS
    local tasks = QNW_TS_Util.tableGet(QNW_TS_ClientCore.taskMap, function(k, v)
        return v.Type == QNW_TS_Fitness.Type
    end)
    for k, v in pairs(tasks) do
        v.count = v.count + time
    end
end

function ISFitnessAction:perform()
    ISFitnessAction_perform(self)
    local time = getGameTime():getCalender():getTimeInMillis() - self.startMS
    local tasks = QNW_TS_Util.tableGet(QNW_TS_ClientCore.taskMap, function(k, v)
        return v.Type == QNW_TS_Fitness.Type
    end)
    for k, v in pairs(tasks) do
        v.count = v.count + time
    end
end

local ISCraftAction_perform = ISCraftAction.perform

function ISCraftAction:perform()
    ISCraftAction_perform(self)
    local type = self.recipe:getModule():getName() .. "." .. self.recipe:getOriginalname()
    if type == "Base.BaoHuFei" then
        local tasks = QNW_TS_Util.tableGet(QNW_TS_ClientCore.taskMap, function(k, v)
            return v.Type == QNW_TS_SawLogs.Type
        end)
        for k, v in pairs(tasks) do
            v.count = v.count + 1
        end
    end
    if type == "Base.Dismantle Digital Watch" then
        local tasks = QNW_TS_Util.tableGet(QNW_TS_ClientCore.taskMap, function(k, v)
            return v.Type == QNW_TS_DismantleDigitalWatch.Type
        end)
        for k, v in pairs(tasks) do
            v.count = v.count + 1
        end
    end
    if type == "Base.DaGeJiaoXian"  then
        local tasks = QNW_TS_Util.tableGet(QNW_TS_ClientCore.taskMap, function(k, v)
            return v.Type == QNW_TS_MakeMetalSheet.Type
        end)
        for k, v in pairs(tasks) do
            v.count = v.count + 1
        end
    end
    if type == "Base.Rip Clothing" then
        local tasks = QNW_TS_Util.tableGet(QNW_TS_ClientCore.taskMap, function(k, v)
            return v.Type == QNW_TS_RipClothing.Type
        end)
        for k, v in pairs(tasks) do
            v.count = v.count + 1
        end
    end
    if type == "Base.BaiNian" then
        local tasks = QNW_TS_Util.tableGet(QNW_TS_ClientCore.taskMap, function(k, v)
            return v.Type == QNW_TS_Salad.Type
        end)                         --BaiNian用Salad注册名，原Salad绑定内容暂被注释
        for k, v in pairs(tasks) do
            v.count = v.count + 1
        end
    end
    if type == "Base.HuJiu" then
        local tasks = QNW_TS_Util.tableGet(QNW_TS_ClientCore.taskMap, function(k, v)
            return v.Type == QNW_TS_HuJiu.Type
        end)
        for k, v in pairs(tasks) do
            v.count = v.count + 1
        end
    end
end
--[[
local ISAddItemInRecipe_perform = ISAddItemInRecipe.perform

function ISAddItemInRecipe:perform()
    local baseItem = self.baseItem:getFullType()
    ISAddItemInRecipe_perform(self)
    local type = self.recipe:getOriginalname()
    local result = self.recipe:getFullResultItem()
    if type == "Make Salad" and result ~= baseItem then
        local tasks = QNW_TS_Util.tableGet(QNW_TS_ClientCore.taskMap, function(k, v)
            return v.Type == QNW_TS_Salad.Type
        end)
        for k, v in pairs(tasks) do
            v.count = v.count + 1
        end
    end
end
]]
local ISPlowAction_perform = ISPlowAction.perform

function ISPlowAction:perform()
    ISPlowAction_perform(self)
    local tasks = QNW_TS_Util.tableGet(QNW_TS_ClientCore.taskMap, function(k, v)
        return v.Type == QNW_TS_Dig.Type
    end)
    for k, v in pairs(tasks) do
        v.count = v.count + 1
    end
end

local ISApplyBandage_perform = ISApplyBandage.perform

function ISApplyBandage:perform()
    ISApplyBandage_perform(self)
    local tasks = QNW_TS_Util.tableGet(QNW_TS_ClientCore.taskMap, function(k, v)
        return v.Type == QNW_TS_Bandage.Type
    end)
    for k, v in pairs(tasks) do
        v.count = v.count + 1
    end
end

local ISUninstallVehiclePart_perform = ISUninstallVehiclePart.perform

function ISUninstallVehiclePart:perform()
    ISUninstallVehiclePart_perform(self)
    local type = self.part:getId()
    if type == "Battery" then
        local tasks = QNW_TS_Util.tableGet(QNW_TS_ClientCore.taskMap, function(k, v)
            return v.Type == QNW_TS_UninstallHeadlight.Type
        end)
        for k, v in pairs(tasks) do
            v.count = v.count + 1
        end
    end
end

local ISFishingAction_start = ISFishingAction.start
local ISFishingAction_stop = ISFishingAction.stop
local ISFishingAction_perform = ISFishingAction.perform

function ISFishingAction:start()
    ISFishingAction_start(self)
    self.startMS = getGameTime():getCalender():getTimeInMillis()
end

function ISFishingAction:stop()
    ISFishingAction_stop(self)
    local time = getGameTime():getCalender():getTimeInMillis() - self.startMS
    local tasks = QNW_TS_Util.tableGet(QNW_TS_ClientCore.taskMap, function(k, v)
        return v.Type == QNW_TS_Fishing.Type
    end)
    for k, v in pairs(tasks) do
        v.count = v.count + time
    end
end

function ISFishingAction:perform()
    ISFishingAction_perform(self)
    local time = getGameTime():getCalender():getTimeInMillis() - self.startMS
    local tasks = QNW_TS_Util.tableGet(QNW_TS_ClientCore.taskMap, function(k, v)
        return v.Type == QNW_TS_Fishing.Type
    end)
    for k, v in pairs(tasks) do
        v.count = v.count + time
    end
end

local ISForageAction_perform = ISForageAction.perform

function ISForageAction:perform()
    ISForageAction_perform(self)
    local tasks = QNW_TS_Util.tableGet(QNW_TS_ClientCore.taskMap, function(k, v)
        return v.Type == QNW_TS_Forage.Type
    end)
    for k, v in pairs(tasks) do
        v.count = v.count + 1
    end
end

local ISCheckTrapAction_perform = ISCheckTrapAction.perform

function ISCheckTrapAction:perform()
    ISCheckTrapAction_perform(self)
    local tasks = QNW_TS_Util.tableGet(QNW_TS_ClientCore.taskMap, function(k, v)
        return v.Type == QNW_TS_Trap.Type
    end)
    for k, v in pairs(tasks) do
        v.count = v.count + 1
    end
end

if QNW_TS_Util.isClient() then
    Events.OnHitZombie.Add(function(_zombie, _player, _bodyPartType, _handWeapon)
        _zombie:getModData().lastPlayerHit = _player:getOnlineID()
    end)
    Events.OnZombieDead.Add(function(_zombie)
        local id = _zombie:getModData().lastPlayerHit
        local player = getPlayer()
        if id == player:getOnlineID() then
            local tasks = QNW_TS_Util.tableGet(QNW_TS_ClientCore.taskMap, function(k, v)
                return v.Type == QNW_TS_Kill.Type
            end)
            for k, v in pairs(tasks) do
                v.count = v.count + 1
            end
            local weapon = player:getPrimaryHandItem()
            if not weapon or not weapon:IsWeapon() then
                return
            end
            local categories = weapon:getCategories()
            if categories:contains("Axe") then
                local tasks = QNW_TS_Util.tableGet(QNW_TS_ClientCore.taskMap, function(k, v)
                    return v.Type == QNW_TS_KillAxe.Type
                end)
                for k, v in pairs(tasks) do
                    v.count = v.count + 1
                end
            end
            if categories:contains("LongBlade") then
                local tasks = QNW_TS_Util.tableGet(QNW_TS_ClientCore.taskMap, function(k, v)
                    return v.Type == QNW_TS_KillLongBlade.Type
                end)
                for k, v in pairs(tasks) do
                    v.count = v.count + 1
                end
            end
            if categories:contains("SmallBlade") then
                local tasks = QNW_TS_Util.tableGet(QNW_TS_ClientCore.taskMap, function(k, v)
                    return v.Type == QNW_TS_KillSmallBlade.Type
                end)
                for k, v in pairs(tasks) do
                    v.count = v.count + 1
                end
            end
            if categories:contains("Blunt") then
                local tasks = QNW_TS_Util.tableGet(QNW_TS_ClientCore.taskMap, function(k, v)
                    return v.Type == QNW_TS_KillBlunt.Type
                end)
                for k, v in pairs(tasks) do
                    v.count = v.count + 1
                end
            end
            if categories:contains("SmallBlunt") then
                local tasks = QNW_TS_Util.tableGet(QNW_TS_ClientCore.taskMap, function(k, v)
                    return v.Type == QNW_TS_KillSmallBlunt.Type
                end)
                for k, v in pairs(tasks) do
                    v.count = v.count + 1
                end
            end
            if categories:contains("Spear") then
                local tasks = QNW_TS_Util.tableGet(QNW_TS_ClientCore.taskMap, function(k, v)
                    return v.Type == QNW_TS_KillSpear.Type
                end)
                for k, v in pairs(tasks) do
                    v.count = v.count + 1
                end
            end
            if weapon:isRanged() then
                local tasks = QNW_TS_Util.tableGet(QNW_TS_ClientCore.taskMap, function(k, v)
                    return v.Type == QNW_TS_KillRanged.Type
                end)
                for k, v in pairs(tasks) do
                    v.count = v.count + 1
                end
            end
        end
    end)
end
